/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.statusChanges;

import java.awt.Window;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.GrantRequest.GrantRequest;
import researchgrants.parts.LoggedData.LoggedData;
import researchgrants.parts.date.DateForDb;
import researchgrants.parts.db.Db;

/**
 * A GrantRequestStatusChange for Submitted grants.
 * Contains the submission date and expected decision date.
 * @author DOStudent1
 */
public class GrantRequestStatusChangeSubmitted extends GrantRequestStatusChange {
    // unique identifiers
    final public static String dbString="Submitted";
    final public static String headerCaption="Submission";

    static public GrantRequestStatusChangeSubmitted getByStatusChangeId(int statusChangeID) {
        return(new GrantRequestStatusChangeSubmitted(statusChangeID));
    }

    // This function creates a new submission and adds it to the given grantRequest.
    static public GrantRequestStatusChangeSubmittedView inputNew(Window parent, GrantRequest grantRequest, ActionListener savedActionListener, ActionListener cancelActionListener) {
        GrantRequestStatusChangeSubmittedView newView=new GrantRequestStatusChangeSubmittedView(parent, grantRequest, savedActionListener, cancelActionListener);
        return(newView);
    }

    private GrantRequestStatusChangeSubmitted(int statusChangeId) {
        super(statusChangeId);
        syncDataFromDb();
    }

    // the logged data
    private LoggedData submissionDate;
    private LoggedData expectedDecisionDate;
    
    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement statusChangeStatement = Db.createPreparedStatement(conn, "SELECT SubmissionDateRef, ExpectedDecisionDateRef FROM tblGrantRequestsStatusChangesSubmitted WHERE GrantRquestStatusChangeID=?");
            statusChangeStatement.setInt(1, getStatusChangeId());
            statusChangeStatement.executeQuery();
            ResultSet statusChangeResultSet = statusChangeStatement.getResultSet();
            Db.moveToFirstLine(statusChangeResultSet);
            submissionDate=LoggedData.getByID((Integer)Db.getColumn(statusChangeResultSet,1));
            expectedDecisionDate=LoggedData.getByID((Integer)Db.getColumn(statusChangeResultSet,2));
            statusChangeResultSet.close();
            statusChangeStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChangeSubmitted.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Could not find id " + getStatusChangeId() + "\n" + ex.getLocalizedMessage());
        }
    }

    // Returns a view for the StatusChangeAwarded
    public GrantRequestStatusChangeView getView(Window parent) {
        return(new GrantRequestStatusChangeSubmittedView(parent, this));
    }

    ///////////////////////////////////////////
    // return the type specific identifiers
    ///////////////////////////////////////////

    public String getStatusChangeTitle() {
        return(headerCaption);
    }

    public String getStatusChangeCode() {
        return(dbString);
    }

    //////////////////////////////////////////////
    // Retuns the current values in fields
    //////////////////////////////////////////////

    public Date getCurrentExpectedDecisionDate() {
        Date res=null;
        String value = expectedDecisionDate.value();
        if (!value.equals("")) {
            try {
                res=DateForDb.fromDbString(value).toDate();
            } catch (ParseException ex) {
                Logger.getLogger(GrantRequestStatusChangeSubmitted.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return(res);
    }

    public Date getCurrentSubmissionDate() {
        Date res=null;
        String value = submissionDate.value();
        if (!value.equals("")) {
            try {
                res=DateForDb.fromDbString(value).toDate();
            } catch (ParseException ex) {
                Logger.getLogger(GrantRequestStatusChangeSubmitted.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return(res);
    }
    
    
    //////////////////////////////////////////////
    // Retuns the raw fields. Used by the view
    //////////////////////////////////////////////

    LoggedData getLoggedDataExpectedDecisionDate() {
        return expectedDecisionDate;
    }

    LoggedData getLoggedDataSubmissionDate() {
        return submissionDate;
    }
}
